package com.bike.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.bike.cache.UserCache;
import com.bike.controller.BaseController;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhVersion;
import com.bike.service.impl.HhAgentServiceImpl;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.service.impl.HhVersionServiceImpl;
import com.bike.util.Constant;
import com.bike.util.Md5Util;
import com.bike.util.PageData;
import com.bike.util.StrUtil;
import com.bike.util.TimeManager;
import com.bike.util.UuidUtil;
import com.topit.show.aliapi.SmsUtil;

 
@Controller
@RequestMapping(value="/apiLogin")
public class HhApiLoginController extends BaseController {
	
		@Autowired
		private HhAgentServiceImpl  hhAgentService;
		@Autowired
		private HhBusinessServiceImpl  hhBusinessServiceImpl;
		@Autowired
		private HhBusinessUserServiceImpl  businessUserServiceImpl;
		@Autowired
		private UserCache  usercache;
		@Autowired
		private HhVersionServiceImpl hhVersionServiceImpl;
	
		/**
		 * 发送手机验证码
		 */
		@RequestMapping("sendCode")
		@ResponseBody
		public PageData sendCode(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			 //发短信
	        SendSmsResponse response;
			try {
				 String code=String.valueOf((int)((Math.random()*9+1)*100000));
				//String code="1234";
				String phone=pd.getString("phone");
				Integer time=pd.getInteger("time");
				String sign=pd.getString("sign");
				
				HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
				if(businessUser!=null){
					phone=businessUser.getPhone();
					if(!StrUtil.isChinaPhoneLegal(phone)){
						return getErrorPd("手机号码格式不对,请修改个人信息");
					}
				}else{
					if(!sign.equals(Md5Util.MD5(Constant.PhoneNumMD5Prefix.concat(phone).concat(time.toString())))){
						return getErrorPd("失败");
					}
				}
				
				
				HhBusinessUser user=businessUserServiceImpl.selectByPhone(phone);
				
//				if(user==null){
//					return getErrorPd("用户不存在");
//				}
				response = SmsUtil.sendLoginCode(phone,code);
				if(response.getCode().equals("OK")){
					usercache.putLoginCodeCacheValue(phone, code);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			PageData successPd=getSuccessPd();
			return  successPd;
		}
		/**
		 * 登录
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("login")
		@ResponseBody
		public PageData login(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			String phone=pd.getString("phone");
			String code=pd.getString("code");
			String pwd=pd.getString("pwd");
			
			if(!code.isEmpty()){
				String codeR=usercache.getLoginCodeCacheValue((phone)).toString();
				if(!StringUtils.isEmpty(codeR)&&code.equals(codeR)){
					usercache.putLoginCodeCacheValue(phone, "");
				}else{
					return getErrorPd("验证码错误");
				}
			}else{
				PageData successPd=getSuccessPd();
				HhBusinessUser user=businessUserServiceImpl.selectByPhone(phone);
				if(user==null){
					return getErrorPd("用户不存在");
				}else if(!user.getPwd().equals(Md5Util.MD5(pwd))){
					return getErrorPd("密码错误");
				}else{
					user.setToken(UuidUtil.get32UUID());
					user.setLoginTime(TimeManager.currentTime());
					businessUserServiceImpl.updateUser(pd, user);
					HhBusiness business=hhBusinessServiceImpl.selectByHhBusinessId(user.getBusinessId());
					successPd.put("data", user);
					successPd.put("businessInfo", business);
				}
				return successPd;
			}
			
			return  getErrorPd("密码错误");
		}
		
		/**
		 * 注册
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("reg")
		@ResponseBody
		public PageData reg(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			PageData successPd=getSuccessPd();
			String phone=pd.getString("phone");//手机号
			String code=pd.getString("code");//手机验证码
			
			HhBusinessUser user= businessUserServiceImpl.selectByPhone(phone);
			if(user!=null){
				return getErrorPd("手机号已经注册过了");
			}
			String codeCache=usercache.getLoginCodeCacheValue((phone)).toString();
			if(!StringUtils.isEmpty(codeCache)&&codeCache.equals(code)){
				hhBusinessServiceImpl.insertRegNewBusiness(pd);
			}
			return successPd;
		}
		/**
		 * 忘记密码
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("rePwd")
		@ResponseBody
		public PageData rePwd(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			PageData successPd=getSuccessPd();
			String phone=pd.getString("phone");//手机号
			String code=pd.getString("code");//联系人
			String pwd=pd.getString("pwd");//新密码
			 
			String codeR=usercache.getLoginCodeCacheValue(phone);
			if(!StringUtils.isEmpty(codeR)&&codeR.equals(code)){
				HhBusinessUser businessUser=businessUserServiceImpl.selectByPhone(phone);
				businessUser.setPwd(Md5Util.MD5(pwd));
				businessUserServiceImpl.updateUser(pd, businessUser);
				return successPd;
			}
			return getErrorPd("验证码不正确");
		}
		/**
		 * version
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("version")
		@ResponseBody
		public PageData version(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			PageData successPd=getSuccessPd();
			List<HhVersion> versions= hhVersionServiceImpl.listAll(successPd);
			HhVersion version=new HhVersion();
			if(versions.size()>0){
				version=versions.get(0);
			}
			successPd.put("version", version.getVersion());//显示的版本号
			successPd.put("build", version.getBuild());//用于验证得版本号
			successPd.put("test", version.getTest());//测试版本号
			successPd.put("note", version.getNote());//说明
			successPd.put("url", version.getUrl());//链接
			return successPd;
		}
}
